package likou.offer;

import java.util.HashMap;
import java.util.Map;

/**
 * @Info:
 * @Author: 唐小尊
 * @Date: 2020/8/30  16:08
 */
public class _56_2 {
    public static void main(String[] args) {
        int[] nums = {3,4,3,3};
        System.out.println(singleNumber2(nums));
    }
    public static int singleNumber(int[] nums) {
        Map<Integer,Integer> map = new HashMap<>();
        for (int i=0;i<nums.length;i++){
            if (!map.containsKey(nums[i])) {
                map.put(nums[i], 1);
            }else if(map.get(nums[i])==2){
                map.remove(nums[i]);
            }else{
                map.put(nums[i],2);
            }
        }
        for (Integer key : map.keySet()){
            return key;
        }
        return -1;
    }
    public static int singleNumber2(int[] nums) {
        int ones = 0, twos = 0;
        for(int num : nums){
            System.out.println("第一步：得到");
            System.out.println("one："+Integer.toBinaryString(num)+"  "+Integer.toBinaryString(ones ^ num)+"  "+Integer.toBinaryString(~twos)+"   "+Integer.toBinaryString(ones ^ num & ~twos));
            ones = ones ^ num & ~twos;

            System.out.println("secondary："+Integer.toBinaryString(num)+"  "+Integer.toBinaryString(twos ^ num)+"  "+Integer.toBinaryString(~ones)+"   "+Integer.toBinaryString(twos ^ num & ~ones));
            twos = twos ^ num & ~ones;
        }
        return ones;
    }
}
